#!/bin/bash
# Piotr Holc
# Script to compile the files, and run the files with their dedicated input, 
# whilst checking and diffing with the provided outputs.

ls | grep '\.alice$' | while read line
do
	
	echo "\n=============================== Working on File: $line ====================================="
	#echo "\tWorking on file: $line" 
	base=`basename $line .alice`
	#echo "\tInvoking compiler..."
	./compiler --O 0 $base.alice  > compilation.oo
	echo $? > compilation.oo
	#echo "----------------\n\tDiff-ing compiler output..."
	diff compilation.oo "$base.00.retvalue" > useless.oo
	if [ $? -eq 1 ]
	then
		echo "\tError: INCORRECT compilation return code"

	#else
		#echo "\tCORRECT compilation return code"
	fi
	#echo "----------------\n\tInvoking $base..."
	./$base < "$base.00.input" > output.oo

	#echo "----------------\n\tDiff-ing the output of the program..."
	diff output.oo "$base.00.output" > useless.oo
	if [ $? -eq 1 ]
	then
		echo "\tError: INCORRECT output"
		#cat useless.oo
	#else
		#echo "\tCORRECT same output"
	fi

	rm $base
	rm $base.bal
done

#clean up
rm useless.oo
rm compilation.oo
rm output.oo
